#ifndef __CC_LOG__
#define __CC_LOG__
#include "cc_serial.h"
#include "cc_bt_serial.h"
#include "cc_core.h"
#include <Arduino.h>

extern int cc_hour;
extern int cc_min;
extern int cc_sec;
extern int time_flag;

#define LOG_D 0
#define LOG_E 1

#define cc_log(format, ...)                                                              \
  do                                                                                     \
  {                                                                                      \
    cc_mutex_lock(serial_tx_mutex);                                                      \
    if (time_flag)                                                                       \
      cc_printf("[%02d:%02d:%02d][log: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                               \
    else                                                                                 \
    {                                                                                    \
      uint32_t tick = cc_timer_get_systick();                                            \
      uint32_t ms = tick / 1000;                                                         \
      cc_hour = ms / 3600 % 24;                                                          \
      cc_min = ms / 60 % 60;                                                             \
      cc_sec = ms % 60;                                                                  \
      cc_printf("[%02d:%02d:%02d][log: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                               \
    }                                                                                    \
    cc_printf(format, ##__VA_ARGS__);                                                    \
    int result = cc_mutex_unlock(serial_tx_mutex);                                       \
  } while (0)

#define cc_log_e(format, ...)                                                              \
  do                                                                                       \
  {                                                                                        \
    if (LOG_E != 1)                                                                        \
    {                                                                                      \
      break;                                                                               \
    }                                                                                      \
    cc_mutex_lock(serial_tx_mutex);                                                        \
    if (time_flag)                                                                         \
      cc_printf("[%02d:%02d:%02d][LOG_E: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                                 \
    else                                                                                   \
    {                                                                                      \
      uint32_t tick = cc_timer_get_systick();                                              \
      uint32_t ms = tick / 1000;                                                           \
      cc_hour = ms / 3600 % 24;                                                            \
      cc_min = ms / 60 % 60;                                                               \
      cc_sec = ms % 60;                                                                    \
      cc_printf("[%02d:%02d:%02d][LOG_E: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                                 \
    }                                                                                      \
    cc_printf(format, ##__VA_ARGS__);                                                      \
    int result = cc_mutex_unlock(serial_tx_mutex);                                         \
  } while (0)

#define cc_log_d(format, ...)                                                                  \
  do                                                                                           \
  {                                                                                            \
    if (LOG_D != 1)                                                                            \
    {                                                                                          \
      break;                                                                                   \
    }                                                                                          \
    cc_mutex_lock(serial_tx_mutex);                                                            \
    if (time_flag)                                                                             \
      cc_printf("[%02d:%02d:%02d][LOG_DEBUG: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                                     \
    else                                                                                       \
    {                                                                                          \
      uint32_t tick = cc_timer_get_systick();                                                  \
      uint32_t ms = tick / 1000;                                                               \
      cc_hour = ms / 3600 % 24;                                                                \
      cc_min = ms / 60 % 60;                                                                   \
      cc_sec = ms % 60;                                                                        \
      cc_printf("[%02d:%02d:%02d][LOG_DEBUG: %s: %04d]:  ", cc_hour, cc_min, cc_sec, __func__, \
                __LINE__);                                                                     \
    }                                                                                          \
    cc_printf(format, ##__VA_ARGS__);                                                          \
    int result = cc_mutex_unlock(serial_tx_mutex);                                             \
  } while (0)
// #define cc_bt_log(format, ...)                                             \
//   do                                                                       \
//   {                                                                        \
//     cc_mutex_lock(serial_tx_mutex);                                        \
//     cc_printf("[log:%07d: %s: %04d]:  ", cc_timer_get_systick(), __func__, \
//               __LINE__);                                                   \
//     Serial.printf(format, ##__VA_ARGS__);                                  \
//     int result = cc_mutex_unlock(serial_tx_mutex);                         \
//   } while (0)

// #define cc_bt_log_e(format, ...)                                       \
//   do                                                                   \
//   {                                                                    \
//     cc_mutex_lock(serial_tx_mutex);                                    \
//     cc_printf("[log_error:%07d: %s: %04d]:  ", cc_timer_get_systick(), \
//               __func__, __LINE__);                                     \
//     Serial.printf(format, ##__VA_ARGS__);                              \
//     int result = cc_mutex_unlock(serial_tx_mutex);                     \
//   } while (0)
#endif